package com.cncf.controller.manage;

import com.cncf.entity.ReportFileInfo;
import com.cncf.response.ResponseData;
import com.cncf.service.ReportFileInfoService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author zj
 */
@Api(value = "Admin", description = "")
@Controller
@RequestMapping("/manage/reportFileInfo")
public class ManageReportFileInfoController {
    @Autowired
    private ReportFileInfoService reportFileInfoService;

    @ApiOperation(value = "通过id查询报告文件", notes = "")
    @RequestMapping(value = "selectReportFileInfoById/{id}", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<ReportFileInfo> selectReportFileInfoById(@ApiParam("报告文件ID") @PathVariable String id){
        return reportFileInfoService.selectReportFileInfoById(id);
    }

    @ApiOperation(value = "分页查询所有报告文件", notes = "不传值不分页")
    @RequestMapping(value = "selectAllReportFileInfoByPage", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<ReportFileInfo>> selectAllReportFileInfoByPage(
            @ApiParam("page") @RequestParam(value = "page",required = false) Integer page,
            @ApiParam("pageSize") @RequestParam(value = "pageSize",required = false) Integer pageSize){
        return reportFileInfoService.selectAllReportFileInfoByPage(page,pageSize);
    }

    @ApiOperation(value = "上传报告文件信息", notes = "")
    @RequestMapping(value = "insertReportFileInfo", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<ReportFileInfo> insertReportFileInfo(
            @ApiParam("id") @RequestParam("id") String id,
            @ApiParam("reportId") @RequestParam("reportId") String reportId,
            @ApiParam("fileName") @RequestParam(value = "fileName",required = false) String fileName,
            @ApiParam("文件存储路径") @RequestParam(value = "path",required = false) String path,
            @ApiParam("flag") @RequestParam(value = "flag",required = false) String flag,
            @ApiParam("creater") @RequestParam(value = "creater",required = false) String creater,
            @ApiParam("memo") @RequestParam(value = "memo",required = false) String memo){
        ResponseData<ReportFileInfo> responseData=new ResponseData<>();
        Object object=reportFileInfoService.selectReportFileInfoById(id).getObj();
        if (object!=null){
            responseData.jsonFill(2,"报告已经存在（id重复）",null);
            return responseData;
        }
        List<ReportFileInfo> list=reportFileInfoService.selectReportFileInfoByReportId(reportId);
        if (list.size()==1){
            responseData.jsonFill(2,"报告已经存在（report_file_info表中的id字段值重复）",null);
            return responseData;
        }
        if (list.size()>1){
            responseData.jsonFill(2,"数据库中存在错误数据（report_file_info表中report_id字段已有重复值",null);
            return responseData;
        }
        ReportFileInfo reportFileInfo=new ReportFileInfo();
        reportFileInfo.setRid(id);
        reportFileInfo.setReportId(reportId);
        reportFileInfo.setFileName(fileName);
        reportFileInfo.setPath(path);
        reportFileInfo.setFlag(flag);
        reportFileInfo.setCreater(creater);
        reportFileInfo.setMemo(memo);
        reportFileInfo.setCreateDateTime(new Date());
        reportFileInfoService.insertReportFileInfo(reportFileInfo);
        responseData.jsonFill(1,null,reportFileInfo);
        return responseData;
    }

}
